package ServletProcess;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import Constant.Constant;

public class checkConncetionToServer {
	public static String process(String currentPhoneNumber) {

		String checkConnection_URL = Constant.CheckConnectionServlet;
		String result = null;

		DefaultHttpClient client = new DefaultHttpClient();
		HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000);
		HttpResponse response;
		HttpEntity entity;
		try {
			HttpPost post = new HttpPost(checkConnection_URL);
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("postContent", currentPhoneNumber));
			post.setHeader("Content-Type", "application/x-www-form-urlencoded");
			post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
			response = client.execute(post);
			entity = response.getEntity();
			result = EntityUtils.toString(entity);
		} catch (Exception e) {
			result = "0";
		}
		return result;
	}
}
